﻿using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ImageUploaderComponent.Data;

namespace ImageUploaderComponent {
    public class ImageHelper {
        private Canvas realRoot;
        public ImageHelper(){

        }

        public BitmapImage GetBmpFromBytes(byte[] imageBytes) {
            var bmp = new BitmapImage();
            MemoryStream memoryStream = new MemoryStream(imageBytes);
            bmp.SetSource(memoryStream);
            return bmp;
        }

        public void showImage(Canvas attachmentRoot, ImageDataViewModel imageData, Point pointOfHover, Size requestedSize)  {
            realRoot = attachmentRoot;
            var display = new ImageDisplay();
            display.ImageBytes = imageData.ImageBytes;
            display.MouseLeave += display_MouseLeave;
            display.MouseLeftButtonUp += display_MouseLeave;
            
            realRoot.Children.Add(display);
            display.ShowYerself(pointOfHover, requestedSize);
            display.AnimateOpen.Begin();

            

        }

        void display_MouseLeave(object sender, MouseEventArgs e) {
            var id = (ImageDisplay)sender;
            id.MouseLeave -= display_MouseLeave;
            id.DataContext = null;
            realRoot.Children.Remove(id);

            id.Visibility = Visibility.Collapsed;
            id.Dispose();


        }
    }
}
